package com.hitqz.robot.driver.zmq.protocol.handler;

import com.example.tutorial.AddressBookProtos;
import com.hitqz.robot.driver.util.QueueStatusEnum;
import com.hitqz.robot.driver.util.WheelCacheUtil;
import com.hitqz.robot.driver.util.WheelToolkit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashSet;

/**
 * Int32数据类型处理类
 *
 * @author wengyx
 * @date 2021-09-14 11:33
 **/
@Slf4j
@Component
public class Int32Handler implements HandlerZMQService {
    @Override
    public void handlerMsg(String robotSn, byte[] data) throws Exception {
        AddressBookProtos.Int32 int32 = AddressBookProtos.Int32.parseFrom(data);
        String type = int32.getDescriptorForType().getName();
        int int32Data = int32.getData();
        int objId = int32.getHeader().getObj();
        int ackId = int32.getHeader().getAck();
        switch (objId) {
            // 定位开关状态 0-关闭 1-开启
            case AddressBookProtos.Int32.OBJ_ID.GET_LOC_STATUS_REP_VALUE -> {
                WheelCacheUtil.put(WheelCacheUtil.KEY_LOC_ENABLE_STATUS, int32Data);
            }
            //充电状态
            //0正常速度控制/1启动回充/2未找到红外信号/3收到充电桩握手信号/4充电正常进行中/5充电过程中出现异常
            //6电池电量充满，充电结束/7充电停止后退/8充电过程中出现拍硬急停或者碰撞开关/9充电极电压检测故障
            //10充电过程调整的距离达到最大值/11充电失败处理中/12充电失败处理完成/13上位机取消充电
            case AddressBookProtos.Int32.OBJ_ID.GET_RECHARGE_STATUS_REP_VALUE, AddressBookProtos.Int32.OBJ_ID.NUL_RECHARGE_STATUS_NTF_VALUE -> {
                WheelCacheUtil.put(WheelCacheUtil.KEY_RECHARGE_STATUS, int32Data);
            }
            // 急停状态，按位组合
            // 0没触发/1软开关开启/2硬急停开启/4碰撞条触发/8充电状态触发
            // 16避障传感器触发（超声波、避障雷达、防跌落、红外避障，不含碰撞条）
            // 32主动轮驱动器故障触发
            case AddressBookProtos.Int32.OBJ_ID.GET_EMERGENCY_STATUS_REP_VALUE,AddressBookProtos.Int32.OBJ_ID.NUL_EMERGENCY_STATUS_NTF_VALUE->{
                HashSet<Integer> set = new HashSet<>();
                if (int32Data != 0) {
                    set.add(int32Data & 0x01);
                    set.add(int32Data & 0x02);
                    set.add(int32Data & 0x04);
                    set.add(int32Data & 0x08);
                    set.add(int32Data & 0x10);
                    set.add(int32Data & 0x20);
                    set.remove(0);
                    //发送消息  只在变化的时候发一次
                    HashSet<Integer> lastStatus =(HashSet<Integer>) WheelCacheUtil.get(WheelCacheUtil.KEY_EMERGENCY_STATUS);
                    HashSet<Integer> sendStatusSet = new HashSet<>(set);
                    if (lastStatus != null && !lastStatus.isEmpty()) {
                        sendStatusSet.removeAll(lastStatus);
                    }
                    //todo 告警 sendStatusSet为当前新的急停状态
                } else {
                    set.add(0);
                }
                WheelCacheUtil.put(WheelCacheUtil.KEY_EMERGENCY_STATUS, int32Data);
            }
            //定位模式  0-AMCL 1-Cartographer, 2-ndt
            case AddressBookProtos.Int32.OBJ_ID.GET_LOC_MODE_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_LOC_MODE, int32Data);
            }
            //导航模式
            //0 空闲（停止当前导航模式）/1 Gmapping 建图/2 Cartographer 建图/3 自主导航/4 巡线导航
            //5 二维码惯性导航/6 磁导航/7 ndtmapping 建图/8 Autoware 寻迹路径采集/9 循迹导航/10 规划导航
            case AddressBookProtos.Int32.OBJ_ID.GET_NAV_MODE_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_NAV_MODE, int32Data);
            }
            //导航雷达避障使能状态 0-关 1-开
            case AddressBookProtos.Int32.OBJ_ID.GET_NAV_LIDAR_AVOID_REP_VALUE->{
                WheelCacheUtil.put(WheelCacheUtil.KEY_NAV_LIDAR_ENABLE_STATUS, int32Data);
            }
            //返回远程连接启用状态

            //NDT 点云地图构建完成后主动上报 并自动回到空闲导航模式

            //二次定位执行状态

            //全局重定位状态反馈

            //局部重定位状态反馈

            //当前定位状态

            //控制模式 0-IPC 导航/上位机  1-手柄
            case AddressBookProtos.Int32.OBJ_ID.NUL_HAND_MODE_NTF_VALUE-> {
                if (int32Data == 1) {
                    log.info("[wheel] 遥控器控制中...");
                }
                WheelCacheUtil.put(WheelCacheUtil.KEY_HAND_MODE, int32Data);
            }
        }
        //释放锁
        WheelToolkit.release(ackId,type, objId, true);
    }



}
